package mock

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
import org.apache.kafka.common.serialization.StringSerializer

import java.util.{Properties, Random}
import scala.collection.mutable.ListBuffer
import scala.util.Random.javaRandomToRandom

object mockData {

  def main(args: Array[String]): Unit = {
    val prop = new Properties()
    prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "node1:9092")
    prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer])
    prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, classOf[StringSerializer])

    val producer = new KafkaProducer[String, String](prop)
    val topic = "resume_topic"

    while (true) {
      mockResumeData().foreach { data =>
        producer.send(new ProducerRecord[String, String](topic, data))
        println(s"Sent: $data")
      }
      Thread.sleep(10000)
    }
  }

  private def mockResumeData(): ListBuffer[String] = {
    val list = ListBuffer[String]()
    val rand = new Random()

    // 中文姓名组件
    val familyNames = Array(
      "李", "王", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴",
      "苏", "唐", "闫", "谢", "韩", "梅", "卫", "蒋", "沈", "朱",
      "秦", "许", "何", "吕", "施", "孔", "曹", "严", "华", "金",
      "魏", "陶", "姜", "戚", "邹", "喻", "柏", "窦", "章", "云",
      "潘", "葛", "奚", "范", "彭", "鲁", "韦", "昌", "马", "苗"
    )
    val givenNames = Array(
      "伟", "芳", "娜", "秀英", "敏", "静", "丽", "强", "磊", "洋",
      "道", "帅", "萍", "峰", "美美", "兰兰", "芳芳", "洁", "勇", "杰",
      "娟", "霞", "明", "超", "秀兰", "燕", "玲", "军", "浩", "宇",
      "鑫", "婷", "雪", "倩", "建国", "建军", "国庆", "建华", "国强", "志强",
      "淑珍", "淑兰", "桂英", "玉兰", "桂兰", "秀珍", "海燕", "红梅", "晓燕", "文静"
    )

    // 选项配置（按照出现顺序排列）
    val genders = Array("男", "女")
    val positions = Array("测试工程师", "后端开发工程师", "前端开发工程师", "全栈开发工程师", "数据分析师",
      "数据工程师", "算法工程师", "移动开发工程师", "云计算工程师", "运维工程师")
    val educations = Array("专科", "本科", "硕士及以上")
    val universityTypes = Array("普通高校", "211高校", "985高校")
    val majors = Array("计算机类", "非计算机类")
    val englishLevels = Array("英语四级", "英语六级", "无")
    val languages = Array("Python", "Java", "JavaScript", "SQL", "Go")
    val expYears = Array("1-3年", "3-5年", "5年以上")
    val results = Array("通过", "不通过")

    // 生成10-30条数据
    for (_ <- 1 to 10 + rand.nextInt(20)) {
      // 按照您要求的严格顺序构建字段
      val fields = Seq(
        "\"resume_id\":" + rand.nextInt(5000),                     // 0-4999
        "\"name\":\"" + familyNames(rand.nextInt(familyNames.length)) +
          givenNames(rand.nextInt(givenNames.length)) + "\"",
        "\"gender\":\"" + genders(rand.nextInt(2)) + "\"",
        "\"age\":" + (21 + rand.nextInt(20)),                     // 21-40
        "\"phone\":\"1" + (1000000000L + rand.nextInt(900000000)) + "\"", // 11位
        "\"target_position\":\"" + positions(rand.nextInt(positions.length)) + "\"",
        "\"education_level\":\"" + educations(rand.nextInt(educations.length)) + "\"",
        "\"university_type\":\"" + universityTypes(rand.nextInt(universityTypes.length)) + "\"",
        "\"major_type\":\"" + majors(rand.nextInt(majors.length)) + "\"",
        "\"english_level\":\"" + englishLevels(rand.nextInt(englishLevels.length)) + "\"",
        "\"programming_languages\":\"" + rand.shuffle(languages.toList)
          .take(1 + rand.nextInt(5))
          .mkString(",") + "\"",
        "\"small_business_experience\":\"" + expYears(rand.nextInt(3)) + "\"",
        "\"middle_business_experience\":\"" + expYears(rand.nextInt(3)) + "\"",
        "\"large_business_experience\":\"" + expYears(rand.nextInt(3)) + "\"",
        "\"small_scale_project\":" + rand.nextInt(16),             // 0-15
        "\"middle_scale_project\":" + rand.nextInt(10),            // 0-9
        "\"large_scale_project\":" + rand.nextInt(3),              // 0-2
        "\"screening_result\":\"" + results(rand.nextInt(2)) + "\""
      )


      val jsonRecord = "{" + fields.mkString(",") + "}"
      list.append(jsonRecord)
    }
    list
  }
}